package grsu.epam.airline.DAOImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import grsu.epam.airline.DAO.ISpecialityDAO;
import grsu.epam.airline.model.Speciality;

public class SpecialityDAOImpl implements ISpecialityDAO {
	private static final String GET_ALL_SPECIALITIES_QUERY = "select speciality_id, speciality_name from speciality";
	private Connection conn;

	public SpecialityDAOImpl(Connection conn) {
		this.conn = conn;
	}

	@Override
	public Speciality getSpecialityById(int id) throws SQLException {
		return null;
	}

	@Override
	public List<Speciality> getAllSpeciality() throws SQLException {
		List<Speciality> specialities = new ArrayList<Speciality>();
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			stmt = conn.prepareStatement(GET_ALL_SPECIALITIES_QUERY);
			rs = stmt.executeQuery();

			while (rs.next()) {
				specialities.add(new Speciality(rs.getInt("speciality_id"), rs
						.getString("speciality_name")));
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (Exception ignore) {
				}
			}
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception ignore) {
				}
			}
		}
		return specialities;
	}
	@Override
	public Connection getConnection() {
		return this.conn;
	}
}
